const aesjs = require('aes-js');
const encrypt = (uid) => {
    const timestamp = Date.now();
    const userid = uid;
    const securityKey = Math.floor(1e8 * Math.random());
    const i = `${timestamp}:${userid}:${securityKey}`;
    let list = [];
    const e = encodeURI(i);
    for (let i = 0; i < e.length; i++) {
        list.push(e.charCodeAt(i));
    }
    list = new Uint8Array(list);

    const a = [68, 107, 79, 108, 105, 87, 118, 70, 78, 82, 55, 67, 52, 87, 118, 82];
    const b = new Uint8Array([71, 81, 87, 75, 85, 69, 50, 67, 86, 71, 79, 79, 66, 75, 88, 85]);
    const key = new Uint8Array(a);
    // console.log("key",aesjs.utils.utf8.fromBytes(key),"iv:",aesjs.utils.utf8.fromBytes(b));

    const aesCfb = new aesjs.ModeOfOperation.cfb(key, b);
    const encryptedBytes = aesCfb.encrypt(list);
   
    const token = aesjs.utils.hex.fromBytes(encryptedBytes);
    return {
        timestamp,
        userid,
        securityKey,
        token
    };
};
const getHeader = (uid) => {
    const {
        timestamp,
        userid,
        securityKey,
        token
    } = encrypt(uid);
    return {
        'x-api-version': '0.0.3',
        'x-user-id': userid,
        'x-request-token': token,
        'x-security-key': securityKey,
        'x-timestamp': timestamp,
        'x-request-misc': {
            "platform": "browser"
        }
    };
};
exports.getHeader = getHeader;


